package com.zjuee.web.ledger.controller;

import com.zjuee.base.bean.response.R;
import com.zjuee.base.constant.Common;
import com.zjuee.base.dto.common.login.LoginDto;
import com.zjuee.base.valid.Insert;
import com.zjuee.base.valid.Update;
import com.zjuee.service.PlanService;
import com.zjuee.service.PlanSolidService;
import com.zjuee.base.model.common.plan.PlanSolid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;

@Slf4j
@RestController
@RequestMapping(Common.Service.ZJUEE_CO_MAIN_PLAN_URL_PREFIX)
public class PlanSolidController  {


    @Resource
    private PlanSolidService planSolidService;
    @Resource
    private PlanService planService;
    @Resource
    private LoginDto loginDto;

    /**
     * 新增固废产生计划信息 PlanSolid
     *
     * @param planSolid PlanSolid
     * @return Long
     */
    @PostMapping("/solid/add")
    public R<Long> addPlanSolid(@Validated(Insert.class) @RequestBody PlanSolid planSolid) {
        if (planSolid.getWpiId() == null) {
            return R.fail("计划ID为空！");
        }
        return R.ok(planSolidService.add(planSolid));
    }

    /**
     * 修改固废产生计划信息 PlanSolid
     *
     * @param planSolid PlanSolid
     * @return Boolean
     */
    @PutMapping("/solid/update")
    public R<Boolean> updatePlanSolid(@Validated(Update.class) @RequestBody  PlanSolid planSolid) {
        if (planSolid.getWpiId() == null) {
            return R.fail("计划ID为空！");
        }
        return R.ok(planSolidService.update(planSolid));
    }

//     
//    public R<Boolean> deletePlanSolid(@NotNull Long id, @NotNull String wpiYear) {
//        Boolean type = false;
//        if (wpiYear.equals(DateUtils.getYear(new Date()))) {
//            type = planSolidService.delete(id, loginDto.getPlanId());
//        } else {
//            PlanInfo plan = planService.selectInfoByBciId(loginDto.getOrgBusinessId(), wpiYear);
//            if (plan != null) {
//                type = planSolidService.delete(id, plan.getId());
//            } else {
//                return R.fail("删除固废计划信息失败");
//            }
//        }
//        if (type) {
//            return R.ok("success");
//        }
//        return R.fail("删除固废计划信息失败");
//    }

    @GetMapping("/solid/get/{id}/{validateId}")
    public R<PlanSolid> selectById(@NotNull @PathVariable(value = "id") Long id, @NotNull @PathVariable(value = "validateId") Long validateId) {
        try {
            PlanSolid planSolid = planSolidService.selectById(id, validateId);
            if (planSolid == null)
                return R.fail("id与validateId不符");
            return R.ok(planSolid);
        } catch (Exception e) {
            return R.fail("id不存在");
        }
    }

    @GetMapping("/solid/getCode")
    public R<List<String>> getCode(Long wpiId,String wpsType) {
        List<String> wpsCodes = planSolidService.getCodeBywpiId(wpiId,wpsType);
        return R.ok(wpsCodes);
    }

    @GetMapping("/solid/getName/{code}")
    public R<String> getNameByCode(@NotNull @PathVariable(value = "code") String code) {
        return null;
    }

    @PostMapping("/solid/getPlanSolid")
    public R<List<PlanSolid>> getPlanSolid(@RequestBody PlanSolid planSolid) {
        return R.ok(planSolidService.getPlanSolid(planSolid));
    }
}
